/*
	物流节点列表
*/

//登录信息
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

//dom
$(function(){
	layui.use(['layer', 'table', 'form'], function(){
		var layer = layui.layer, table = layui.table, form = layui.form;

		//ajaxData
		var ajaxData = { aId: adminLoginedInfo.aId, aToken: adminLoginedInfo.aToken };

		//获取节点列表
		function getFreightList(abbr){
			/*
				获取全部物流节点信息
			*/
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = {}
			abbr ? _ajaxData = $.extend(ajaxData, {abbr: abbr}) : _ajaxData = ajaxData;

			$.ajax({
				url: BASE_SERVICEURL + URlS.freight,
				type: 'GET',
				data: _ajaxData,
				success: function(responseData){
					var code = responseData.msg.code;
					if(code == 200){
						//console.log(responseData)
						createTable(responseData);
						layer.close(loadIndex)
					}else if(code == 2005){
						//登录失效
						layer.msg('登录失效，请重新登陆后重试', {time: 2000, end: function(){
							parent.layer.close(layer.index);
                        	window.parent.location.reload();
						}});
					}else if(code == 2002){
						layer.msg('接口缺少必要参数，请确认数据均以填写后重试！', {time: 2000});
						layer.close(loadIndex)
					}
				},
				error: function(responseData){
					layer.msg(MESSAGES.service_error , { time: 2000, end: function(){
                    	window.location.href = './500.html';
                    }});
				}
			})
		};

		// 构建table
		function createTable(responseData){
			//组织表格数据
			var data = responseData.data;
			var tableData = [];
			for (var i = data.length - 1; i >= 0; i--) {
				//倒叙
				tableData.push({
					abbr: data[i].abbr,
					name: data[i].name,
					name_en: data[i].name_en,
					freight_cn: data[i].single_freight['cn'],
					freight_tw: data[i].single_freight['tw'],
					freight_en: data[i].single_freight['en'],
					standard_cn: data[i].free_freight_standard['cn'],
					standard_tw: data[i].free_freight_standard['tw'],
					standard_en: data[i].free_freight_standard['en'],
					status: data[i].status
				})
			}
			//构建表格
			table.render({
				elem: '#tableContent',
				limit: 20, //分页
				height: 620,
				page: true,
				cols:[[ //表头
					{field: 'abbr', title: '节点代码', sort: true, fixed: 'left', width:120},
					{field: 'name', title: '节点名（中文）', sort: true},
					{field: 'name_en', title: '节点名（英文）', sort: true} ,
					{field: 'freight_cn', title: '单件运费（人民币）'},
					{field: 'freight_tw', title: '单件运费（新台币）'},
					{field: 'freight_en', title: '单件运费（美元）'},
					{field: 'standard_cn', title: '免运标准（人民币）'},
					{field: 'standard_tw', title: '免运标准（新台币）'},
					{field: 'standard_en', title: '免运标准（美元）'},
					{field: 'status', title: '状态（0：不应用，1：应用）', sort: true},
					{field: 'option', title: '操作',width:120, toolbar:'#toolbar'}
				]],
				data:tableData
			});
		}

		//编辑查看
		table.on('tool(node-list-table)', function(obj){
			var data = obj.data;   //本行数据
			var layEvent = obj.event; 
			var tr = obj.tr;  //当前行
			//console.log(data)
			if(layEvent == 'editor'){  //查看详情
				x_admin_show('编辑修改', 'freight-node-detail.html?abbr='+ data.abbr +'', 1200, 800);
			};
		});

		//高级检索
		form.on('submit(search)', function(data){
			//console.log(data);
			var f = data.field;
			getFreightList(f.nodeID);
			return false;
		})
		
		getFreightList()
	});
})